482A - Diverse Permutation - CodeForces Solution


constructive algorithms greedy *1200

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <bitset>
#include <algorithm>
using namespace std;

#define ll long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vll vector<long long>
#define mii map<int, int>
#define si set<int>
#define sc set<char>
#define pq priority_queue<long long>
#define lb lower_bound
#define ub upper_bound
#define f(i,s,e) for(long long int i=s;i<e;i++)
#define cf(i,s,e) for(long long int i=s;i<=e;i++)
#define rf(i,e,s) for(long long int i=e-1;i>=s;i--)
#define pb push_back
#define eb emplace_back
#define MOD 1000000007
#define PI 3.1415926535897932384626433832795
#define read(type) readInt<type>()
#define np next_permutation

int main(){
    ll n,k;
    cin>>n>>k;
    for(int i=1;i<=n-k;i++)
    {
        cout<<i<<" ";
    }
    int p=0;
    int a=n;
    int b=n-k+1;
    while(k--)
    {
        if(p%2==0)
        {
            cout<<a<<" ";
            p++;
            a--;
        }
        else
        {
            cout<<b<<" ";
            p++;
            b++;
        }
    }
}


Comments

Submit
0 Comments
More Questions

Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations
1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array